package com.surfmanager.ws;

import com.google.gson.Gson;
import com.surfmanager.dao.LoginDAO;
import com.surfmanager.model.User;
import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 *
 * @author eamiker
 */
@WebService(serviceName = "LoginWS3")
public class LoginWS3 {

    /**
     * This is a sample web service operation
     */
    @WebMethod(operationName = "hello")
    public String hello(@WebParam(name = "name") String txt) {
        return "Hello " + txt + " !";
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "addUser")
    public String addUser(@WebParam(name = "name") String name, @WebParam(name = "password") String password, @WebParam(name = "email") String email) {
       
        User user = new User(name, password, email);
        LoginDAO loginDao = new LoginDAO();
        return loginDao.addUser(user);
        
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getUser")
    public String getUser(@WebParam(name = "id") int id) {
       
        previousOperation();
        LoginDAO loginDao = new LoginDAO();
       return loginDao.getUser(id);
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getAllUsers")
    public List<User> getAllUsers() {
        
        previousOperation();
        LoginDAO loginDao = new LoginDAO();
        return loginDao.getALLUsers();
    }
    
     @WebMethod(operationName = "getAllUsersJson")
    public String getAllUsersJson() {
        
        previousOperation();
        LoginDAO loginDao = new LoginDAO();
        Gson gson = new Gson();
        List<User> users = loginDao.getALLUsers();
        return gson.toJson(users);
        
    }

    private void previousOperation() {
         User user = new User("nnn", "nnn", "nnn@gmail.com");
          LoginDAO loginDao = new LoginDAO();
         int id = loginDao.addUserWithId(user);
         loginDao.remove(id);
    }
}
